﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using RedeSocial.DataAccess;

namespace RedeSocial.Web
{
	public class Global : System.Web.HttpApplication
	{

		protected void Application_Start(object sender, EventArgs e)
		{
			using (var dataContext = new RedeSocialEntities())
			{
				Application.Add("Languages", dataContext.Languages.Where(l => l.IsActive).OrderBy(l => l.Name).AsQueryable<Language>().ToList<Language>());
				Application.Add("DefaultLanguage", dataContext.Settings.Where(s => s.IsActive).Select(l => l.Language).FirstOrDefault());
				if (Application["DefaultLanguage"] == null)
					Application["DefaultLanguage"] = (Application["Languages"] as List<Language>).FirstOrDefault();
			}
		}

		protected void Session_Start(object sender, EventArgs e)
		{
			Session.Add("DefaultLanguage", Application["DefaultLanguage"]);

		}

		protected void Application_BeginRequest(object sender, EventArgs e)
		{

		}

		protected void Application_AuthenticateRequest(object sender, EventArgs e)
		{

		}

		protected void Application_Error(object sender, EventArgs e)
		{

		}

		protected void Session_End(object sender, EventArgs e)
		{
			Session.Remove("DefaultLanguage");
			if (Session["Session"] != null)
				Session.Remove("Session");
		}

		protected void Application_End(object sender, EventArgs e)
		{
			Application.Remove("Languages");
			Application.Remove("DefaultLanguage");
		}
	}
}